package com.yiren.manager.ctrl.pension;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.StringUtil;
import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.utils.Favorites;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrBedCost;
import com.yiren.entity.YrBedCostHistory;
import com.yiren.entity.YrHouseType;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.pension.YrBedCostForm;
import com.yiren.manager.service.iface.beds.YrHouseTypeService;
import com.yiren.manager.service.iface.pension.YrBedCostService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("bedCost")
@Controller
public class YrBedCostcontroller extends GenericController<YrBedCostForm, YrBedCost> {

	@Autowired
	private YrBedCostService yrBedCostService;

	@Autowired
	private YrHouseTypeService YrHouseTypeService;

	// 保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrBedCostForm> save(String data)
			throws SwallowException, MyException, InstantiationException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException, IntrospectionException {
		HTTPResult<YrBedCostForm> result = new HTTPResult<YrBedCostForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrBedCostForm form = tool.mapper("bedCost", YrBedCostForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if (null == form.getBedCostId() || form.getBedCostId().isEmpty()) {
			form.setBedCostId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());

		// 实体
		YrBedCost entity = ObjectUtils.objectToObject(form, YrBedCost.class);
		// 修改历史
		YrBedCostHistory bedCostHistory = ObjectUtils.objectToObject(entity, YrBedCostHistory.class);

		Favorites favorites = new Favorites();
		favorites.putFavorite(YrBedCostHistory.class, bedCostHistory);
		favorites.putFavorite(YrBedCost.class, entity);

		yrBedCostService.addByFavorites(favorites);

		return result;
	}

	/** 获取所有记录 */
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrBedCostForm> findAll() throws MyException, InstantiationException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException {
		HTTPResult<YrBedCostForm> result = new HTTPResult<YrBedCostForm>();
		List<YrBedCost> entityList = yrBedCostService.findAll();
		List<YrBedCostForm> formList = new ArrayList<YrBedCostForm>();
		for (YrBedCost bed : entityList) {
			YrBedCostForm form = ObjectUtils.objectToObject(bed, YrBedCostForm.class);
			YrHouseType houseType = YrHouseTypeService.findOne(form.getHouseTypeId());
			if (null != houseType) {
				form.setHousrTypeName(houseType.getHouseType());
			}
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}

	/** 删除一条记录 */
	@RequestMapping("delete")
	@ResponseBody
	public HTTPResult<YrBedCostForm> delete(String id) throws MyException {
		HTTPResult<YrBedCostForm> result = new HTTPResult<YrBedCostForm>();
		if(StringUtil.isEmpty(id)){
			throwExp("param.error");
		}
		yrBedCostService.delete(id);
		return result;
	}

	/** 获取一条记录 */
	@RequestMapping("findOne")
	@ResponseBody
	public HTTPResult<YrBedCostForm> findOne(String id) throws MyException, InstantiationException,
			IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		HTTPResult<YrBedCostForm> result = new HTTPResult<YrBedCostForm>();
		if(StringUtil.isEmpty(id)){
			throwExp("param.error");
		}
		YrBedCost entity = yrBedCostService.findOne(id);
		List<YrBedCostForm> formList = new ArrayList<YrBedCostForm>();

		YrBedCostForm form = ObjectUtils.objectToObject(entity, YrBedCostForm.class);

		YrHouseType houseType = YrHouseTypeService.findOne(form.getHouseTypeId());
		if (null != houseType) {
			form.setHousrTypeName(houseType.getHouseType());
		}
		formList.add(form);

		result.setData(formList);
		return result;
	}

	/**
	 * 批量删除
	 * @param data
	 * @return
	 * @throws SwallowException
	 * @throws MyException
	 */
	@RequestMapping("deleteBatch")
	@ResponseBody
	public HTTPResult<YrBedCostForm> deleteBatch(String data) throws SwallowException, MyException{
		HTTPResult<YrBedCostForm> result = new HTTPResult<YrBedCostForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		String[] ids = tool.mapper("ids", String[].class);
		if(null==ids){
			throwExp("param.error");
		}
		for(String id : ids){
			yrBedCostService.delete(id);
		}
		return result;
	}
	
	@Override
	public YrBedCostForm toForm(YrBedCost e) throws Exception {
		YrBedCostForm form = ObjectUtils.objectToObject(e, YrBedCostForm.class);
		return form;
	}

	@Override
	public YrBedCost toEntity(YrBedCostForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
